# -*- coding: utf8 -*-
import os

#ported from http://fayaa.com/code/view/35/full/

def print_nt(foreground, newline, *kw):
    from ctypes import windll, Structure, c_short, c_uint, byref
    
    #+8 means highlight
    cc_map = {
        'black': 0,
        'darkblue': 1,
        'darkgreen': 2,
        'darkcyan': 3,
        'darkred': 4,
        'darkmagenta': 5,
        'brown': 6, #dark yellow
        'darkwhite': 7,
        'blue': 9,
        'green': 10,
        'cyan': 11,
        'red': 12,
        'magenta': 13,
        'yellow': 14,
        'white': 15,
        }
    
    CloseHandle = windll.kernel32.CloseHandle
    GetStdHandle = windll.kernel32.GetStdHandle
    GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
    SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute

    STD_OUTPUT_HANDLE = -11

    class COORD(Structure):
        _fields_ = [('X', c_short), ('Y', c_short)]

    class SMALL_RECT(Structure):
        _fields_ = [('Left', c_short),
                   ('Top', c_short),
                   ('Right', c_short),
                   ('Bottom', c_short),
                  ]
                   
    class CONSOLE_SCREEN_BUFFER_INFO(Structure):
        _fields_ = [('dwSize', COORD),
                   ('dwCursorPosition', COORD),
                   ('wAttributes', c_uint),
                   ('srWindow', SMALL_RECT),
                   ('dwMaximumWindowSize', COORD),
                  ]

    if foreground in cc_map:
        hconsole = GetStdHandle(STD_OUTPUT_HANDLE)
        scrinfo = CONSOLE_SCREEN_BUFFER_INFO()
        GetConsoleScreenBufferInfo(hconsole, byref(scrinfo))
        oldcolor = scrinfo.wAttributes
        SetConsoleTextAttribute(hconsole, cc_map[foreground])
    
    for t in kw: print t,
    if newline: print
    
    if foreground in cc_map:
        SetConsoleTextAttribute(hconsole, oldcolor)

#code source: http://nick.workao.org/index.php/linux/692        
def print_inx(foreground, newline, *kw):
    cc_map = {
        'black': '30',
        'darkred': '31',
        'darkgreen': '32',
        'brown': '33', #dark yellow
        'darkblue': '34',
        'darkmagenta': '35',
        'darkcyan': '36',
        'darkwhite': '37',
        'red': '1;31',
        'green': '1;32',
        'yellow': '1;33',
        'blue': '1;34',
        'magenta': '1;35',
        'cyan': '1;36',
        'white': '1;37',
    }
    if foreground in cc_map:
        for t in kw:
            print '\033[' + cc_map[foreground] + 'm{0}\033[0m'.format(t),
    else:
        for t in kw: print t,
    
    if newline: print
    
def print_colorful(foreground, newline, *kw):
    try:
        if foreground == 'darkyellow':
            foreground = 'brown'
            
        if os.name == 'nt':
            print_nt(foreground, newline, *kw)
        else:
            print_inx(foreground, newline, *kw)
    except:
#         for t in kw: print t,
        if newline: print

if __name__ == '__main__':
    #print_colorful('red', 'red')
    
    for c in ['red', 'green', 'cyan', 'blue', 'yellow', 'magenta', 'white']:
        print_colorful(c, True, 'test for ' + c)
        print_colorful('dark' + c, True, 'test for dark' + c)